/*

  The MIT License

  Copyright (c) 2008 - 2011. Dimarik

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.

*/

package ru.goodcore.display.ui.core
{

import flash.events.Event;
import flash.geom.Point;

import ru.goodcore.datastructure.Enum;
import ru.goodcore.display.ui.Container;

/**
 *  @author				Dimarik
 *  @version			1.1
 *  @langversion		3.0
 *  @playerversion		9.0
 * 
 */
public class UIComponent extends InvalidableSprite {

	//-------------------------------------------------------------------------
	//
	//   Constructor
	//
	//-------------------------------------------------------------------------

	public function UIComponent() {
		super();
		this.$widthDependent = 
		this.$heightDependent = SizeDependet.CHILD;
	}

	//-------------------------------------------------------------------------
	//
	//   Variables & const
	//
	//-------------------------------------------------------------------------

	/**
	 * @private
	 * "Внутренний" размер компонента. Этот размер применяется при рендеринге.
	 */	
	protected const size:			Point = new Point();
	protected const percentSize:	Point = new Point();

	//-------------------------------------------------------------------------
	//
	//   Overridden properties:	DisplayObject
	//
	//-------------------------------------------------------------------------

	//=====================================================
	//   Explicite Size 
	//=====================================================

	//---------------------------------
	//  width
	//---------------------------------
	[PercentProxy("percentWidth")]
	/**
	 * @param value
	 * Устанавливается точный размер внешними объектами, не родителем.
	 */
	public override function set width(value:Number):void {
		if ( isNaN(value) ) {
			this.$widthDependent = SizeDependet.CHILD;
		} else {
			this.$widthDependent = SizeDependet.EXPLICIT;
			if ( int( (value - this.size.x) * 10) ) {
				this.size.x = value;
				this.sizeChanged = true;
				super.invalidate();
			}
		}
	}

	public override function get width():Number {
		return this.size.x;
	}

	//---------------------------------
	//  percentWidth
	//---------------------------------
	/** 
	 * @param value
	 * Устанавливается пропорциональный размер, любыми объектами.
	 */
	public function set percentWidth(value:Number):void {
		this.$widthDependent = SizeDependet.PARENT;
		if (this.percentSize.x === value) return;
		this.percentSize.x = value;
		this.sizeChanged = true;
		super.invalidate();
	}

	public function get percentWidth():Number {
		return this.percentSize.x;
	}

	//---------------------------------
	//  height
	//---------------------------------

	public override function set height(value:Number):void {
		if ( isNaN(value) ) {
			this.$widthDependent = SizeDependet.CHILD;
		} else {
			this.$heightDependent = SizeDependet.EXPLICIT;			
			if ( int( (value - this.size.y) * 10) ) {
				this.size.y = value;
				this.sizeChanged = true;
				super.invalidate();
			}
		}
	}

	public override function get height():Number {
		return this.size.y;
	}

	//-------------------------------------------------------------------------
	//
	//   Properties
	//
	//-------------------------------------------------------------------------

	//---------------------------------
	//  widthDependent
	//---------------------------------
	/**
	 * По умолчанию размер компонента зависит от размера его детей 
	 */
	protected var $widthDependent:	SizeDependet;

	protected function get widthDependent():SizeDependet {
		return this.$widthDependent;
	}

	//---------------------------------
	//  heightDependent
	//---------------------------------
	/**
	 * По умолчанию размер компонента зависит от размера его детей 
	 */
	protected var $heightDependent:	SizeDependet;

	protected function get heightDependent():SizeDependet {
		return this.$heightDependent;
	}

	//---------------------------------
	//  explicitWidth
	//---------------------------------
	/**
	 * @private
	 */
	private var _explicitWidth:		Number = 0;

	protected function set explicitWidth(value:Number):void {
		this._explicitWidth = value;
	}

	protected function get explicitWidth():Number {
		return this._explicitWidth;
	}

	//---------------------------------
	//  explicitHeight
	//---------------------------------
	/**
	 * @private
	 */
	private var _explicitHeight:		Number = 0;

	public function set explicitHeight(value:Number):void {
		this._explicitHeight = value;
	}

	public function get explicitHeight():Number {
		return this._explicitHeight;
	}

	//---------------------------------
	//  style
	//---------------------------------
	/**
	 * @private
	 */
	protected var $style:	Object;

	public function set style(value:Object):void {
		this.$style = value;
		this.styleChanged = true;
	/*
		if ( !style ) {
			this.widthDependent = SizeBehavior.CHILD;
			this.heightDependent = SizeBehavior.CHILD;
		}
	*/

		if ( !isNaN(style.left & style.right) ) {
			this.$widthDependent = SizeDependet.PARENT;
		}

		if ( !isNaN(style.top & style.bottom) ) {
			this.$heightDependent = SizeDependet.PARENT;
		}

		super.invalidate();
	}

	public function get style():Object {
		return this.$style;
	}

	//-------------------------------------------------------------------------
	//
	//   Private variables
	//
	//-------------------------------------------------------------------------

	/**
	 * @private 
	 */	
	protected var sizeChanged:	Boolean = false;

	/**
	 * @private 
	 */	
	protected var styleChanged:	Boolean = false;

	//-------------------------------------------------------------------------
	//
	//   Overridden methods: InvalidableSprite
	//
	//-------------------------------------------------------------------------

	public override function invalidate():void {
		if (this.sizeChanged) {
			super.dispatchEvent( new Event(Event.RESIZE, true) );
		}

		super.invalidate();
	}

	/**
	 * @private
	 */
	public override function validate():void {
		super.validate();
		this.sizeChanged = false;
		this.styleChanged = false;
	}

	//-------------------------------------------------------------------------
	//
	//   Private methods
	//
	//-------------------------------------------------------------------------

	//-------------------------------------------------------------------------
	//
	//   Event handlers
	//
	//-------------------------------------------------------------------------

}
}